﻿#include "WingedTigerLord.h"
#include "../../../Shared/Functions/Functions.h"
#include "../DelayedAction.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../MapObject.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Enums.h"
#include "../../MirEnvir/Map.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    WingedTigerLord::WingedTigerLord(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool WingedTigerLord::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void WingedTigerLord::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        DelayedAction *action;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        int damage = 0;

        if (ranged)
        {
            if (tornado)
            {
                S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                tempVar->Type = 0;
                tempVar->TargetID = getTarget()->ObjectID;
                Broadcast(tempVar);

                damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

                std::vector<MapObject*> targets = FindAllTargets(1, getTarget()->getCurrentLocation());

                for (int i = 0; i < targets.size(); i++)
                {
                    action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 1000, {targets[i], damage, DefenceType::ACAgility});
                    ActionList.push_back(action);
                }

                ActionTime = getEnvir()->getTime() + 800;
                AttackTime = getEnvir()->getTime() + AttackSpeed;

                tornado = false;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
                return;
            }
        }

        if (!ranged)
        {
            if (stomp)
            {
                //Foot stomp
                S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->Type = 2;
                Broadcast(tempVar2);

                MirDirection dir = Functions::PreviousDir(getDirection());
                Point *tar;
                Cell *cell;

                damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

                for (int i = 0; i < 8; i++)
                {
                    tar = Functions::PointMove(getCurrentLocation(), dir, 1);
                    dir = Functions::NextDir(dir);

                    if (!getCurrentMap()->ValidPoint(tar))
                    {
                        continue;
                    }

                    cell = getCurrentMap()->GetCell(tar);

                    if (cell->Objects.empty())
                    {
                        continue;
                    }

                    for (int o = 0; o < cell->Objects.size(); o++)
                    {
                        MapObject *ob = cell->Objects[o];
                        if (ob->getRace() != ObjectType::Player && ob->getRace() != ObjectType::Monster)
                        {
                            continue;
                        }
                        if (!ob->IsAttackTarget(this))
                        {
                            continue;
                        }

                        action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility, AttackType::Stomp});
                        ActionList.push_back(action);
                        break;
                    }
                }

                ActionTime = getEnvir()->getTime() + 800;
                AttackTime = getEnvir()->getTime() + AttackSpeed;

                stomp = false;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
                return;
            }

            switch (getEnvir()->Random->Next(2))
            {
                case 0:
                    //Slash
                    S::ObjectAttack *tempVar3 = new S::ObjectAttack();
                    tempVar3->ObjectID = ObjectID;
                    tempVar3->Direction = getDirection();
                    tempVar3->Location = getCurrentLocation();
                    tempVar3->Type = 0;
                    Broadcast(tempVar3);

                    damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                    action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility, AttackType::SingleSlash});
                    ActionList.push_back(action);

                    damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                    action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::ACAgility, AttackType::SingleSlash});
                    ActionList.push_back(action);
                    break;
                case 1:
                    //Two hand slash
                    S::ObjectAttack *tempVar4 = new S::ObjectAttack();
                    tempVar4->ObjectID = ObjectID;
                    tempVar4->Direction = getDirection();
                    tempVar4->Location = getCurrentLocation();
                    tempVar4->Type = 1;
                    Broadcast(tempVar4);

                    damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                    action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility, AttackType::SingleSlash});
                    ActionList.push_back(action);
                    break;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar4' statement was not added since tempVar4 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
            }

            if (getEnvir()->Random->Next(5) == 0)
            {
                stomp = true;
            }

            if (getEnvir()->Random->Next(2) == 0)
            {
                tornado = true;
            }
        }

        ActionTime = getEnvir()->getTime() + 500;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
    }

    void WingedTigerLord::CompleteRangeAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        int poisonTime = GetAttackPower(Stats[Stat::MinSC], Stats[Stat::MaxSC]);

        if (target->Attacked(this, damage, defence) <= 0)
        {
            return;
        }

        PoisonTarget(target, 2, poisonTime, PoisonType::Dazed, 2000);
    }

    void WingedTigerLord::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);
        AttackType type = std::any_cast<AttackType>(data[3]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        if (target->Attacked(this, damage, defence) <= 0)
        {
            return;
        }

        switch (type)
        {
            case AttackType::Stomp:
            {
                    PoisonTarget(target, 2, 5, PoisonType::Paralysis, 2000);
            }
                break;
        }
    }
}
